//
//  ViewController.m
//  TopMenu
//
//  Created by liwei on 2017/6/6.
//  Copyright © 2017年 liwei. All rights reserved.
//

#import "ViewController.h"
#import "MenuTopView.h"
#import "MenuListManager.h"
#import "MenuManageController.h"

//屏幕宽度
#define kScreenW [UIScreen mainScreen].bounds.size.width
//屏幕高度
#define kScreenH [UIScreen mainScreen].bounds.size.height

@interface ViewController ()<UIScrollViewDelegate>
//顶部标签栏
@property (nonatomic,strong) MenuTopView *menuTopView;

//内容容器
@property (nonatomic,strong) UIScrollView *containerView;

//当前所用到的工具类
@property (nonatomic,strong) MenuListManager *currentManager;

//顶部标签数据
@property (nonatomic,strong) NSMutableArray *menuTopData;

//列表数组
@property (nonatomic,strong) NSMutableArray *menulistArray;

//工具类数组
@property (nonatomic,strong) NSMutableArray *menuManageArray;

//标识是滚动内容scrollView
@property (nonatomic,assign) BOOL containerScroll;



@end

@implementation ViewController

- (NSMutableArray *)menuTopData {

    if (!_menuTopData) {
        _menuTopData = @[@"全部",@"倒立",@"塑性",@"排毒",@"巩固",@"舒缓平静",@"力量",@"肩颈修复",@"零基础",@"夜间"].mutableCopy;
    }
    return _menuTopData;
}

- (UIScrollView *)containerView {

    if (!_containerView) {
        _containerView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(self.menuTopView.frame), kScreenW, kScreenH - CGRectGetMaxY(self.menuTopView.frame))];
        _containerView.backgroundColor = [UIColor clearColor];
        _containerView.pagingEnabled = YES;
        _containerView.showsVerticalScrollIndicator = NO;
        _containerView.showsHorizontalScrollIndicator = NO;
        _containerView.delegate = self;
        _containerView.contentSize = CGSizeMake(kScreenW * self.menuTopData.count, 0);
        [self.view addSubview:_containerView];
        
        [self.view bringSubviewToFront:self.menuTopView];
    }
    return _containerView;
}

- (NSMutableArray *)menulistArray {

    if (!_menulistArray) {
        _menulistArray = [NSMutableArray array];
        for (int i = 0; i < self.menuTopData.count; i++) {
            UITableView *recommentTab = [[UITableView alloc] initWithFrame:CGRectMake( i * kScreenW, 0, kScreenW, self.containerView.frame.size.height) style:UITableViewStylePlain];
            recommentTab.backgroundColor = [UIColor whiteColor];
            recommentTab.separatorStyle = UITableViewCellSeparatorStyleNone;
            [self.containerView addSubview:recommentTab];
            [_menulistArray addObject:recommentTab];
        }
    }
    return _menulistArray;
}

- (NSMutableArray *)menuManageArray {
    
    if (!_menuManageArray) {
        _menuManageArray = [NSMutableArray array];
        for (int i = 0; i < self.menuTopData.count; i++) {
            MenuListManager *manager = [[MenuListManager alloc] init];
            manager.menulistTab = self.menulistArray[i];//这两步主要是绑定tagModel 和 对应的列表
            [_menuManageArray addObject:manager];
        }
    }
    return _menuManageArray;
}

- (MenuTopView *)menuTopView {
    
    if (!_menuTopView) {
        _menuTopView = [[MenuTopView alloc] initWithFrame:CGRectMake(0, 20, kScreenW, 44)];
        [self.view addSubview:_menuTopView];
    }
    return _menuTopView;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor lightGrayColor];
        
    [self createTopView];
}

//顶部操作栏的定制
- (void)createTopView {
    
    [self menuTopView];
    
    

    __weak typeof(self) weakSelf = self;
    
    self.menuTopView.updateContentOffset = ^(int index) {
        weakSelf.containerScroll = NO;
        [weakSelf.containerView setContentOffset:CGPointMake(kScreenW * index, 0) animated:YES];
    };

    self.menuTopView.didClickMenuTop = ^(NSInteger index) {
        weakSelf.containerScroll = NO;
        [weakSelf.containerView setContentOffset:CGPointMake(index * kScreenW, 0) animated:YES];
        weakSelf.currentManager = weakSelf.menuManageArray[index];
        weakSelf.currentManager.menuName = weakSelf.menuTopData[index];
        [weakSelf.currentManager loadListData];
        
    };
    
    self.menuTopView.clickMenuColumn = ^{
        MenuManageController *menuManage = [[MenuManageController alloc] init];
        
        [weakSelf presentViewController:menuManage animated:YES completion:nil];
        
    };
    
    self.menuTopView.dataSource = self.menuTopData;

    
    
}

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
    self.containerScroll = YES;
    
}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView {
    
    if (!self.containerScroll) {
        return;
    }
    //如果滚动到了半屏幕，就改变顶部的偏移量
    int index = scrollView.contentOffset.x / scrollView.frame.size.width;
    if (scrollView.contentOffset.x - index * scrollView.frame.size.width >= scrollView.frame.size.width * 0.5) {//表明是滑动到下一页，超过一半了
        [self.menuTopView updateScrollOffsetWith:(scrollView.contentOffset.x / scrollView.frame.size.width + 1)];
    }
    
    if (index * scrollView.frame.size.width - scrollView.contentOffset.x  >= - scrollView.frame.size.width * 0.5) {
        [self.menuTopView updateScrollOffsetWith:(scrollView.contentOffset.x / scrollView.frame.size.width)];
    }
    
}


- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    
    int index = scrollView.contentOffset.x / kScreenW;
    //改变顶部的偏移量
    [self.menuTopView updateScrollOffsetWith:index];
    [self.menuTopView triggerTopItemRequestWith:index];
}



@end
